package com.evil.common.application.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 查询类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum QueryTypeEnum {

    /**
     * 查询类型
     */
    ACCURATE(0, "精确查询"),
    FUZZY(1, "模糊查询"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<QueryTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static QueryTypeEnum isExistById(Integer id) {
        return QueryTypeEnum.findById(id).orElseThrow(() ->
                new BusinessException(RCodeEnum.ERROR_PARAMETER, "查询类型参数错误"));
    }

    public static boolean isFuzzy(Integer id) {
        return QueryTypeEnum.findById(id).map(e -> FUZZY.id == e.id).orElse(true);
    }
}
